﻿namespace Sedodream.Web.Common.View.HtmlView
{
    using System;
    using System.Globalization;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Mvc.Html;  // required for extension methods

    public class HtmlText : AbstractHtmlViewObject
    {
        private readonly string mLabelText;
        private readonly bool mCreateLabel;
        private readonly object mValue;
        private readonly string mValidationMessage;
        private readonly bool mCreateValidationMessage;
        private readonly bool mCreateLi;
        private readonly bool mReadonly;

        public HtmlText(
            IViewRequestContext requestContext, string name, string labelText, object value, 
            string validationMessage, bool @readonly, bool createLi, object attributes)
            : base(requestContext, name)
        {
            mLabelText = labelText;
            mCreateLabel = !string.IsNullOrEmpty(mLabelText);
            mValidationMessage = validationMessage;
            mCreateValidationMessage = !string.IsNullOrEmpty(validationMessage);
            mCreateLi = createLi;
            mReadonly = @readonly;
            Attributes = attributes;

            object valueToAssign = value;
            if (valueToAssign == null)
            {
                // see if the ModelState has a value for this
                valueToAssign = GetModelStateValue(name, typeof(string));
            }

            mValue = valueToAssign;
        }
           
        public override void StartView()
        {
            HttpResponseBase httpResponse = RequestContext.HttpResponse;

            TagBuilder liTagBuilder = new TagBuilder("li");
            if (mCreateLi)
            {
                httpResponse.Write(liTagBuilder.ToString(TagRenderMode.StartTag));
            }

            // write out label if provided
            if (mCreateLabel)
            {
                TagBuilder labelTag = new TagBuilder("label");
                labelTag.Attributes.Add("for", Name);
                labelTag.SetInnerText(mLabelText);
                httpResponse.Write(labelTag.ToString(TagRenderMode.Normal));
            }

            string stringValue = string.Empty;
            if (this.mValue != null)
            {
                stringValue = Convert.ToString(this.mValue, CultureInfo.CurrentCulture);
            }

            if (this.mReadonly)
            {
                TagBuilder textTag = new TagBuilder("span");
                textTag.AddCssClass("readonly-text");
                textTag.SetInnerText(
                    Convert.ToString(this.mValue, CultureInfo.CurrentCulture));
                httpResponse.Write(textTag.ToString(TagRenderMode.Normal));
            }
            else
            {
                // Use MVC helpers to create the actual text box
                httpResponse.Write(RequestContext.HtmlHelper.TextBox(
                    Name, this.mValue, Attributes));
            }

            if (this.mCreateLi)
            {
                httpResponse.Write(liTagBuilder.ToString(TagRenderMode.EndTag));
            }
        }

        public override void EndView()
        {
            // Not needed for this element
        }
    }
}